import tkinter as tk
from tkinter import filedialog as fd, messagebox
import tdms2txt
import txt2xlsx


class Window:
    def __init__(self, master=None):
        self.main_window = tk.Tk()
        self.main_window.overrideredirect(True)
        self.top_frame = tk.Frame(self.main_window)
        self.result_dict = {
            "resource_path": "",
            "save_path": str(),

        }
        self.buttons = [
            "tdms转txt",
            "txt转xlsx",
            "tdms截取",
            "txt文件拼接",
            "txt文件截取",
            "tdms绘图",
            "txt 绘图",
            "xlsx绘图"
        ]
        for index, button in enumerate(self.buttons):
            btnx = tk.Button(self.top_frame, text=button, width=12,
                             command=lambda name=button: self.callback_consoler(name))
            btnx.grid(row=index, column=0, padx=10, pady=5)
        btnq = tk.Button(self.top_frame, text="退出", width=12, command=quit)
        btnq.grid(row=len(self.buttons) + 1, column=0, padx=10, pady=5)
        self.top_frame.grid(row=0, column=0, padx=10, pady=10)

    # 中间件
    def callback_consoler(self, name):
        '''
        中间件，用于管理所有的回调函数
        :param name:
        :return:
        '''
        if name == self.buttons[0]:
            new_window = tk.Toplevel(self.main_window)
            new_window.title(name)
            self.callback_tdms_txt(new_window)
        if name == self.buttons[1]:
            new_window = tk.Toplevel(self.main_window)
            new_window.title(name)
            self.callback_txt_xlsx(new_window)
        if name == self.buttons[2]:
            new_window = tk.Toplevel(self.main_window)
            new_window.title(name)
            self.callback_tdms_cut(new_window)

    def _askopenfilenames(self, ftype, fextend_name):
        resource_path = fd.askopenfilenames(filetypes=[
            (f"{ftype}", f"*{fextend_name}"),
            ("All Files", "*.*")
        ])
        if resource_path == '':
            messagebox.showerror('Error', 'Please select tdms file at least one.')
            pass
        self.result_dict['resource_path'] = resource_path
        print(resource_path)

    def _asksavedirectory(self):
        save_path = fd.askdirectory()
        if save_path == '':
            messagebox.showerror('Error', 'Please select a directory for save txt files.')
            pass
        self.result_dict['save_path'] = save_path

    def callback_tdms_txt(self, master):
        varstring1 = tk.StringVar()
        varstring2 = tk.StringVar()
        lable1 = tk.Label(master, text="Tdms转txt")
        lable2 = tk.Label(master, text="数据集")
        entry1 = tk.Entry(master, textvariable=varstring1, width=60)
        button1 = tk.Button(master, text="选择文件", command=lambda: self._askopenfilenames("Tdms Files", ".tdms"))
        lable3 = tk.Label(master, text="保存位置")
        entry2 = tk.Entry(master, textvariable=varstring2, width=60)
        button2 = tk.Button(master, text="选择位置", command=self._asksavedirectory)
        button3 = tk.Button(master, text="开始转换", command=self.func1)

        lable1.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
        lable2.grid(row=1, column=0, columnspan=1, padx=10, pady=10)
        entry1.grid(row=1, column=1, columnspan=1, padx=10, pady=10)
        button1.grid(row=1, column=2, columnspan=1, padx=10, pady=10)
        lable3.grid(row=2, column=0, columnspan=1, padx=10, pady=10)
        entry2.grid(row=2, column=1, columnspan=1, padx=10, pady=10)
        button2.grid(row=2, column=2, columnspan=1, padx=10, pady=10)
        button3.grid(row=3, column=0, columnspan=3, padx=10, pady=10)

        varstring1.set(self.result_dict.get("resource_path"))
        varstring2.set(self.result_dict.get("save_path"))
        master.update()

    def func1(self):
        # 实例化转换器
        convert = tdms2txt.Converter(self.result_dict.get("resource_path"), self.result_dict.get("save_path"))
        convert.set_process()

        # 转换完成
        messagebox.showinfo("Complete!", "Comission done!")

    def callback_txt_xlsx(self, master):
        self.String2.set("")
        # frame1
        lable1 = tk.Label(master, text="txt转excel")
        lable2 = tk.Label(master, text="数据集")
        entry1 = tk.Entry(master, width=60, textvariable=self.String2)
        button1 = tk.Button(master, text="选择文件", command=self._txt_xlsx)
        lable3 = tk.Label(master, text="首列表头")
        entry2 = tk.Entry(master, width=5)
        lable4 = tk.Label(master, text="首列单位")
        entry3 = tk.Entry(master, width=5)

        lable1.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

        lable2.grid(row=3, column=0, columnspan=1, padx=10, pady=10)
        entry1.grid(row=3, column=1, columnspan=2, padx=10, pady=10)
        button1.grid(row=3, column=3, columnspan=1, padx=10, pady=10)

        lable3.grid(row=2, column=0, columnspan=1, padx=10, pady=10)
        entry2.grid(row=2, column=1, columnspan=1, padx=10, pady=10)
        entry3.grid(row=2, column=3, columnspan=1, padx=10, pady=10)
        lable4.grid(row=2, column=2, columnspan=1, padx=10, pady=10)

    def callback_tdms_cut(self, master):
        self.str_resource_file = tk.StringVar()
        self.str_content_name = tk.StringVar()

        lable = tk.Label(master, text="Tdms截取")
        lable1 = tk.Label(master, text="数据集")
        entry1 = tk.Entry(master, width=60, textvariable=self.str_resource_file)
        button1 = tk.Button(master, text="选择文件", command=self._tdms_cut)
        lable_name = tk.Label(master, text="文件名：")
        content_name = tk.Label(master, textvariable=self.str_content_name)

        lable.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
        lable1.grid(row=1, column=0, columnspan=1, padx=10, pady=10)
        entry1.grid(row=1, column=1, columnspan=1, padx=10, pady=10)
        button1.grid(row=1, column=2, columnspan=1, padx=10, pady=10)
        lable_name.grid(row=2, column=0, columnspan=1, padx=10, pady=10)
        content_name.grid(row=2, column=1, columnspan=1, padx=10, pady=10)

    def _txt_xlsx(self):
        directory = fd.askopenfilenames(filetypes=[
            ("Text Files", "*.txt"),
            ("All Files", "*.*")
        ])
        if directory == '':
            messagebox.showerror('Error', 'Please select at least one file.')
            pass
        else:
            self.String2.set(directory[0])
            tct_d = txt2xlsx.Converter(txt_file_directory=directory, absu=False)
            tct_d.load_txt_directory()
            tct_d.head = {
                'Time(s)': 's',
            }
            messagebox.showinfo("Complete!", "Comission done!")

    def _tdms_cut(self):
        pass

    def start(self):
        self.main_window.mainloop()


if __name__ == '__main__':
    my_window = Window()
    my_window.start()
